﻿Imports MICROFORMA.BEL
Imports MICROFORMA.BLL



Public Class frmListLotes


    Dim _LoteBL As New LoteBL
    Dim _LoteBE As New LoteBE


    Dim _DetalleLoteBL As New DetalleLoteBL
    Dim _DetalleLoteBE As New DetalleLoteBE


    Private Sub lnkNewIndice_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkNewIndice.LinkClicked
        Dim _frmAddLotes As New frmAddLotes
        _frmAddLotes.ShowDialog()
        Call LoadList()
        _frmAddLotes.Dispose()
    End Sub


    Private Sub LoadList()
        lstvIndices.Items.Clear()

        Dim _datableIndices As DataTable = _LoteBL.GetList
        Dim lviItem As ListViewItem

        For Each dtrFila As DataRow In _datableIndices.Rows
            lviItem = New ListViewItem
            lviItem.Text = dtrFila("IdLote")
            lviItem.SubItems.Add(dtrFila("Nombre"))
            lviItem.SubItems.Add(dtrFila("Cliente"))
            lviItem.SubItems.Add(dtrFila("fechacreacion"))
            lstvIndices.Items.Add(lviItem)
        Next

    End Sub


    Private Sub lnkUpdateIndice_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkUpdateIndice.LinkClicked
        If lstvIndices.SelectedItems.Count > 0 Then
            Call ShowFormListAdd(True, CShort(lstvIndices.SelectedItems(0).Text.Trim))
        End If
        Call LoadList()
    End Sub

    Private Sub ShowFormListAdd(Optional ByVal bolUpdate As Boolean = Nothing, Optional ByVal idIndice As Short = 0)

        Dim _frmAddSolicitud As New frmAddSolicitud
        _frmAddSolicitud.bolUpdate = bolUpdate
        _frmAddSolicitud.pIdSolicitud = CShort(lstvIndices.SelectedItems(0).Text.Trim)

        _frmAddSolicitud.ShowDialog()
        _frmAddSolicitud.Dispose()

    End Sub


    Private Sub lnkEliminar_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkEliminar.LinkClicked
        If lstvIndices.SelectedItems.Count > 0 Then
            Call DeleteDataExternal(lstvIndices.SelectedItems(0).Text)
            Call LoadList()
        Else
            MessageMistake("Por favor Seleccione un Registro.")
            lstvIndices.Focus()
        End If
    End Sub

    Private Sub DeleteDataExternal(ByVal Id As Integer)
        If MessageMakeAction("Esta Seguro de eliminar el Registro Seleccionado.") Then

            Dim msg As String = _LoteBL.Delete(Id)
            If Not msg = "OK" Then
                MessageMistake(msg)
            End If
        End If
    End Sub

    Private Sub DeleteDataExternalDetalle(ByVal Id As Integer)
        If MessageMakeAction("Esta Seguro de eliminar el Registro Seleccionado.") Then

            Dim msg As String = _DetalleLoteBL.Delete(Id)
            If Not msg = "OK" Then
                MessageMistake(msg)
            End If
        End If
    End Sub

    Private Sub CargarDetalle(ByVal Id As Integer)
        lstDetalle.Items.Clear()

        Dim _datableIndices As DataTable = _DetalleLoteBL.List_IdLote(Id)
        Dim lviItem As ListViewItem

        For Each dtrFila As DataRow In _datableIndices.Rows
            lviItem = New ListViewItem
            lviItem.Text = dtrFila("IdElemento")
            lviItem.SubItems.Add(dtrFila("Documento"))
            lviItem.SubItems.Add(dtrFila("Contenedor"))
            lviItem.SubItems.Add(dtrFila("CantidadDocumentos"))
            lviItem.SubItems.Add(dtrFila("CantidadFolios"))

            lstDetalle.Items.Add(lviItem)
        Next


        
    End Sub

    Private Sub frmListLotes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadList()
    End Sub


    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)

    End Sub

    Private Sub LinkLabel6_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel6.LinkClicked
        If lstDetalle.SelectedItems.Count > 0 Then
            Call DeleteDataExternalDetalle(lstDetalle.SelectedItems(0).Text)
            LinkLabel1_LinkClicked(Nothing, Nothing)
        Else
            MessageMistake("Por favor Seleccione un Registro.")
            lstDetalle.Focus()
        End If
    End Sub

    Private Sub SplitContainer1_Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer1.Panel1.Paint

    End Sub

    Private Sub lstvIndices_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstvIndices.SelectedIndexChanged
        If lstvIndices.SelectedItems.Count = 0 Then Exit Sub
        Dim id As Integer = lstvIndices.SelectedItems(0).Text
        Call CargarDetalle(id)
    End Sub

    Private Sub LinkLabel5_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel5.LinkClicked

    End Sub

    Private Sub LinkLabel4_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel4.LinkClicked
        Dim _frmAddDetalleLote As New frmAddDetalleLote
        Dim DetalleLoteBE As BEL.DetalleLoteBE
        _frmAddDetalleLote.ItemDetalle = DetalleLoteBE
        _frmAddDetalleLote.BolSoloItem = True
        _frmAddDetalleLote.ShowDialog()
        DetalleLoteBE = _frmAddDetalleLote.ItemDetalle

        'Agrega el item

        If DetalleLoteBE Is Nothing Then Exit Sub

        Dim StrIdLote As String = lstvIndices.SelectedItems(0).Text
        Dim blDetalleLote As New DetalleLoteBL
        DetalleLoteBE.IdLote = StrIdLote
        blDetalleLote.Add(DetalleLoteBE)
        
        Call CargarDetalle(StrIdLote)
        _frmAddDetalleLote.Dispose()
    End Sub
End Class